import { getAnalyze, getAnalyzeLarge, getClbn, getTopRank } from '@/server/controllers/secuController';
import { formatDateTime } from '@/lib/date-time';
import { fetchTopRank } from '@/lib/kpl';

export const dynamic = 'force-dynamic';

const SearchPage = async () => {
    let result = await getAnalyze();
    let top_ranks = await getTopRank(4);
    let largeLtsz = await getAnalyzeLarge();
    let allNeps = result.map((r) => r.neps || []).flat();
    let eyes = new Set([...allNeps, ...top_ranks]);
    // largeLtsz.forEach((secu) => {
    //     secu.links = secu.tokens.filter((t) => eyes.has(t));
    //     console.log(secu.links);
    // });
    // console.log(result);
    const now = new Date();
    return (
        <div className="flex flex-col">
            <div>{formatDateTime(now)}</div>
            <p className="text-xs" style={{ color: 'red' }}>
                精选：{top_ranks.join('；')}
            </p>
            {/*<p className="text-xs" style={{ color: 'red' }}>*/}
            {/*    neps：{allNeps.join('；')}*/}
            {/*</p>*/}

            <div>
                {largeLtsz.map((secu) => {
                    return (
                        <p className="text-xs border-b-2" key={secu.secu_code}>
                            {secu.secu_name} ：
                            <span style={{ color: 'cyan' }}> {secu.tokens.filter((t) => eyes.has(t)).join('；')}</span>
                            <br />
                            {secu.vib.map((t) => {
                                const b1s = result.filter((r) => {
                                    const b1_ranks = r.vib_ranks || [];
                                    return b1_ranks.includes(t);
                                });
                                if (b1s.length === 0) return;
                                return (
                                    <>
                                        <span style={{ color: 'red' }}>
                                            <span style={{ color: 'yellow' }}> （{b1s.length}）</span>
                                            <span>{t}</span>
                                        </span>
                                        ：<span>{b1s.map((b) => b.secu_name).join('+')}</span>
                                        <br />
                                    </>
                                );
                            })}
                        </p>
                    );
                })}
            </div>
        </div>
    );
};

export default SearchPage;
